//
//  ABExcute.m
//  afnetworkingtest
//
//  Created by appbegin on 14-2-10.
//  Copyright (c) 2014年 appbegin. All rights reserved.
//

#import "SEExcute.h"
#import "AFNetworking.h"
#import "ZAlertControlTool.h"
#import "LLYErrorManager.h"

#import "SGProgressHUD.h"
#import "UserModel.h"
#import "JQProgressHUD.h"
#import "VDJConstant.h"

#define kStatusCods @"code"

@interface SEExcute()
{
    
}
/**
 The string encoding used in constructing url requests. This is `NSUTF8StringEncoding` by default.
 */
@property (nonatomic, assign) NSStringEncoding stringEncoding;
@end

@implementation SEExcute

- (void)excutePostWithPath:(NSString *)path withParam:(NSDictionary *)dictionary hasHeader:(BOOL)hasHeader onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    if (hasHeader) {
        UserModel * model = [UserModel queryModel];
        AFHTTPRequestSerializer * header = [manager requestSerializer];
        [header setValue:[NSString stringWithFormat:@"token %@",model.token] forHTTPHeaderField:@"Authorization"];
    }
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;//[operation.responseString JSONValue];
        NSUInteger statusCode = [[dic objectForKey:kStatusCods] integerValue];
        if (statusCode == 200) {
            successBlcok(dic);
        }else{
            
            NSString *msg = [dic objectForKey:@"message"];
            if (msg) {
                [JQProgressHUD showErrorMessage:msg];
            }
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        
        failureBlock(error);
    }];
}

- (void)excutePostWithPath:(NSString *)path withParam:(NSDictionary *)dictionary hasHeader:(BOOL)hasHeader noHud:(BOOL)noHud onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    if (hasHeader) {
        UserModel * model = [UserModel queryModel];
        AFHTTPRequestSerializer * header = [manager requestSerializer];
        [header setValue:[NSString stringWithFormat:@"token %@",model.token] forHTTPHeaderField:@"Authorization"];
    }
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        NSDictionary *dic = responseObject;//[operation.responseString JSONValue];
        NSUInteger statusCode = [[dic objectForKey:kStatusCods] integerValue];
        if (statusCode == 200) {
            successBlcok(dic);
        }else{
            if (!noHud) {//有提示
                NSString *msg = [dic objectForKey:@"message"];
                if ([[dictionary valueForKey:@"page"] integerValue] == 1) {
                    
                    if (![@"没有更多作业了" isEqualToString:msg]) {
                        if (msg) {
                            [JQProgressHUD showSuccessMessage:msg];
                        }
                    }
                }else{
                    if (msg) {
                        [JQProgressHUD showErrorMessage:msg];
                    }
                }
            }
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

- (void)excutePostWithPath:(NSString *)path withParam:(NSDictionary *)dictionary onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;//[operation.responseString JSONValue];
        NSUInteger statusCode = [[dic objectForKey:kStatusCods] integerValue];
        if (statusCode == 200) {
            successBlcok(dic);
        }else{
            NSString *msg = [dic objectForKey:@"message"];
            if (msg) {
                [JQProgressHUD showErrorMessage:msg];
            }
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

- (void)excutePostReturnArrayWith:(NSString *)path withParam:(NSDictionary *)dictionary onSuccess:(ABResultsArraySuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.securityPolicy.allowInvalidCertificates = YES;
    AFHTTPRequestSerializer * header = [manager requestSerializer];
    [header setValue:@"weidianjing" forHTTPHeaderField:@"token"];
    [header setValue:@"1" forHTTPHeaderField:@"ostype"];
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;//[operation.responseString JSONValue];
        NSDictionary *headDic = [dic objectForKey:@"H"];
        if ([[headDic valueForKey:@"E"] integerValue] == 1000) {
            NSArray *bodyArray= [dic objectForKey:@"B"];
            successBlcok(bodyArray);
        }else{
            LLYErrorManager * manager = [[LLYErrorManager alloc] init];
            [ZAlertControlTool showHUB:[manager httpsErrorMsgByErrorCode:[headDic valueForKey:@"E"]]];
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

- (void)excutePostReturnArrayWith:(NSString *)path hasHeader:(BOOL)hasHeader withParam:(NSDictionary *)dictionary onSuccess:(ABResultsArraySuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.securityPolicy.allowInvalidCertificates = YES;
    if (hasHeader) {
        UserModel * model = [UserModel queryModel];
        AFHTTPRequestSerializer * header = [manager requestSerializer];
        [header setValue:[NSString stringWithFormat:@"token %@",model.token] forHTTPHeaderField:@"Authorization"];
    }
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;//[operation.responseString JSONValue];
        NSDictionary *headDic = [dic objectForKey:@"H"];
        if ([[headDic valueForKey:@"E"] integerValue] == 1000) {
            NSArray *bodyArray= [dic objectForKey:@"B"];
            successBlcok(bodyArray);
        }else{
            LLYErrorManager * manager = [[LLYErrorManager alloc] init];
            [ZAlertControlTool showHUB:[manager httpsErrorMsgByErrorCode:[headDic valueForKey:@"E"]]];
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

- (void)excutePutWithPath:(NSString *)path withParam:(NSDictionary *)dictionary onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    MyLog(@"rpath = %@",path);
    [manager PUT:path parameters:dictionary success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;
        successBlcok(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

- (void)excuteGetWithPath:(NSString *)path withParam:(NSDictionary *)dictionary onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    MyLog(@"rpath = %@",path);
    [manager GET:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;
        successBlcok(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

- (void)excuteGetWithPath:(NSString *)path onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    MyLog(@"excuteGet path = %@",path);
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    MyLog(@"rpath = %@",path);
    [manager GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;
        successBlcok(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

// AFMultipartFormData
- (void)excuteFormDataPostWithPath:(NSString *)path withParam:(NSDictionary *)dictionary hasHeader:(BOOL)hasHeader onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;
        NSUInteger statusCode = [[dic objectForKey:kStatusCods] integerValue];
        if (statusCode == 200) {
            successBlcok(dic);
        }else{
            
            NSString *msg = [dic objectForKey:@"message"];
            if (msg) {
                [JQProgressHUD showErrorMessage:msg];
            }
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

- (void)excuteFormDataPostWithPath:(NSString *)path withParam:(NSDictionary *)dictionary hasHeader:(BOOL)hasHeader noHud:(BOOL)noHud onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    if (hasHeader) {
        UserModel * model = [UserModel queryModel];
        AFHTTPRequestSerializer * header = [manager requestSerializer];
        [header setValue:[NSString stringWithFormat:@"token %@",model.token] forHTTPHeaderField:@"Authorization"];
    }
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;//[operation.responseString JSONValue];
        NSUInteger statusCode = [[dic objectForKey:kStatusCods] integerValue];
        if (statusCode == 200) {
            successBlcok(dic);
        }else{
            if (!noHud) {//有提示
                NSString *msg = [dic objectForKey:@"message"];
                if ([[dictionary valueForKey:@"page"] integerValue] == 1) {
                    
                    if (![@"没有更多作业了" isEqualToString:msg]) {
                        if (msg) {
                            [JQProgressHUD showSuccessMessage:msg];
                        }
                    }
                }else{
                    if (msg) {
                        [JQProgressHUD showErrorMessage:msg];
                    }
                }
            }
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

// 上传头像
- (void)excuteUserHeaderPostWithPath:(NSString *)path withParam:(NSDictionary *)dictionary withFile:(NSData *)file onSuccess:(ABResultsSuccessBlock)successBlcok onFailure:(ABFailureBlock)failureBlock {
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    MyLog(@"rpath = %@",path);
    [manager POST:path parameters:dictionary constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [formData appendPartWithFormData:[dictionary[@"sign"] dataUsingEncoding:NSUTF8StringEncoding] name:@"sign"];
        [formData appendPartWithFormData:[dictionary[@"ti"] dataUsingEncoding:NSUTF8StringEncoding] name:@"ti"];
        [formData appendPartWithFileData:file name:@"userfile" fileName:@"file.jpg" mimeType:@""];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyLog(@"responseObject ===xxx = %@",responseObject);
        MyLog(@"statusCode ===xxx = %ld",(long)[[responseObject objectForKey:kStatusCods] integerValue]);
        
        NSDictionary *dic = responseObject;
        NSUInteger statusCode = [[dic objectForKey:kStatusCods] integerValue];
        if (statusCode == 200) {
            successBlcok(dic);
        }else{
            
            NSString *msg = [dic objectForKey:@"message"];
            if (msg) {
                [JQProgressHUD showErrorMessage:msg];
            }
            //出错。
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"error= =%@",error);
        failureBlock(error);
    }];
}

@end
